package com.dgut.lotterySystem.service.activityStatus.operator;

import com.dgut.lotterySystem.dao.dataObject.ActivityUserDO;
import com.dgut.lotterySystem.dao.mapper.ActivityUserMapper;
import com.dgut.lotterySystem.service.dto.ConvertActivityStatusDTO;
import com.dgut.lotterySystem.service.enums.ActivityUserEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
public class UserOperator extends AbstractActivityOperator{
    @Autowired
    private ActivityUserMapper activityUserMapper;
    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId=convertActivityStatusDTO.getActivityId();
        List<Long> userIds=convertActivityStatusDTO.getUserIds();
        ActivityUserEnum activityUserEnum=convertActivityStatusDTO.getTargetActivityUserStatus();
        if(activityId==null
                ||CollectionUtils.isEmpty(userIds)
                ||activityUserEnum==null){
            return false;
        }

        List<ActivityUserDO> userDOS=activityUserMapper.batchSelectByAUId(activityId,userIds);
        if(CollectionUtils.isEmpty(userDOS)){
            return false;
        }
        for (ActivityUserDO activityUserDO : userDOS) {
            if(activityUserDO.getStatus()
                    .equalsIgnoreCase(activityUserEnum.name())){
                return false;
            }
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId=convertActivityStatusDTO.getActivityId();
        List<Long> userIds=convertActivityStatusDTO.getUserIds();
        ActivityUserEnum activityUserEnum=convertActivityStatusDTO.getTargetActivityUserStatus();
        try{
            activityUserMapper.updateStatus(activityId,userIds,activityUserEnum.name());
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
